#Tales of Fantasy Skem parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class TalesOfFantasy_skem(Model3D):
    '''Replace 'TalesOfFantasy_skem' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TalesOfFantasy_skem, self).__init__("skem")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        self.inFile.read_long(3)
        charLen = self.inFile.read_short()
        meshName = self.inFile.read_string(charLen)
        self.create_object(meshName)
        
        self.inFile.read_long()
        numVerts = self.inFile.read_long()
        for i in range(numVerts):            
            vx, vz, vy = self.inFile.read_float(3)
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
        numVerts = self.inFile.read_long()
        for i in range(numVerts):
            nx, ny, nz = self.inFile.read_float(3)
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            
        numVerts = self.inFile.read_long()
        for i in range(numVerts):
            tu, tv = self.inFile.read_float(2)
            
        numVerts = self.inFile.read_long()
        for i in range(numVerts):
            self.inFile.read_float(3)
            
        numVerts = self.inFile.read_long()
        for i in range(numVerts):
            self.inFile.read_float(4)
            
        numVerts = self.inFile.read_long()
        for i in range(numVerts):
            self.inFile.read_float(4)
            
        faceNum = self.inFile.read_long() / 3
        print faceNum
        for i in range(faceNum):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
    
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = TalesOfFantasy_skem(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "SKEM", "Tales of Fantasy skem"

if __name__ == '__main__':
    
    file1 = "WS_N_Xiangzi01.skem"
    read_file(file1)